﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;

namespace HCI.Questionaire.Controls
{
    public partial class MediaPlayer : Canvas
    {
        public event CallbackDelegate PlayClicked;
        public event MouseEventHandler NextMouseEnter;
        public event MouseEventHandler NextMouseLeave;

        public event MouseEventHandler PreviousMouseEnter;
        public event MouseEventHandler PreviousMouseLeave;

        public MediaPlayer()
        {
            InitializeComponent();


            //Stream s = this.GetType().Assembly.GetManifestResourceStream("WindowsMediaPlayer.ucMediaPlayer.xaml");
            //oRootElement = this.InitializeFromXaml(new StreamReader(s).ReadToEnd());

            //canMediaButtons = oRootElement.FindName("canMediaButtons") as Canvas;
            //bottomFooterBkgStrip = oRootElement.FindName("bottomFooterBkgStrip") as FrameworkElement;
            //bottomFooterBkgStripGlass = oRootElement.FindName("bottomFooterBkgStripGlass") as FrameworkElement;
            //bottomFooterButtonBkg = oRootElement.FindName("bottomFooterButtonBkg") as FrameworkElement;
            //canButtons = oRootElement.FindName("canButtons") as Canvas;
            //bottomFooterButtonBkgCircleBkg = oRootElement.FindName("bottomFooterButtonBkgCircleBkg") as FrameworkElement;
            //canPlayStopButton = oRootElement.FindName("canPlayStopButton") as Canvas;
            //canMediaProgress = oRootElement.FindName("canMediaProgress") as Canvas;

            //sbPlayStopGlow_Start = ((DependencyObject)oRootElement.Resources).FindResource("sbPlayStopGlow_Start") as Storyboard;
            //sbPlayStopGlow_Stop = oRootElement.Resources.FindName("sbPlayStopGlow_Stop") as Storyboard;

            //this.sbPlayMouseOver

            canBackCopy.MouseEnter += new System.Windows.Input.MouseEventHandler(canBackCopy_MouseEnter);
            canBackCopy.MouseLeave += new MouseEventHandler(canBackCopy_MouseLeave);
            canBackCopy.MouseLeftButtonUp += new MouseButtonEventHandler(canBackCopy_MouseLeftButtonUp);

            this.btnNext.MouseEnter += new System.Windows.Input.MouseEventHandler(canPlayStopButton_MouseEnter);
            this.btnNext.MouseLeave += new MouseEventHandler(canPlayStopButton_MouseLeave);
            this.btnNext.MouseLeftButtonUp += new MouseButtonEventHandler(canPlayStopButton_MouseLeftButtonUp);
        }

        void canPlayStopButton_MouseLeftButtonUp(object sender, System.Windows.Input.MouseEventArgs e)
        {
            sbPlayStopGlow_Stop.Stop();
            sbPlayStopGlow_Start.Stop();
            sbPlayStopGlow_Start.Begin();

            sbPlayStopGlow_Start.Stop();
            sbPlayStopGlow_Stop.Stop();
            sbPlayStopGlow_Stop.Begin();

            //this.OnClick("Clicked", "Next");
        }

        void canPlayStopButton_MouseLeave(object sender, EventArgs e)
        {
            sbPlayStopGlow_Start.Stop();
            sbPlayStopGlow_Stop.Stop();
            sbPlayStopGlow_Stop.Begin();
        }

        void canPlayStopButton_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            sbPlayStopGlow_Stop.Stop();
            sbPlayStopGlow_Start.Stop();
            sbPlayStopGlow_Start.Begin();
        }

        void canBackCopy_MouseLeave(object sender, EventArgs e)
        {
            sbCanBackCopy_Start.Stop();
            sbCanBackCopy_Stop.Stop();
            sbCanBackCopy_Stop.Begin();


        }

        void canBackCopy_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            sbCanBackCopy_Stop.Stop();
            sbCanBackCopy_Start.Stop();
            sbCanBackCopy_Start.Begin();

            sbCanBackCopy_Start.Stop();
            sbCanBackCopy_Stop.Stop();
            sbCanBackCopy_Stop.Begin();

            //this.OnClick("MouseEnter", "Previous");
        }
        void canBackCopy_MouseLeftButtonUp(object sender, System.Windows.Input.MouseEventArgs e)
        {
            sbCanBackCopy_Stop.Stop();
            sbCanBackCopy_Start.Stop();
            sbCanBackCopy_Start.Begin();

            sbCanBackCopy_Start.Stop();
            sbCanBackCopy_Stop.Stop();
            sbCanBackCopy_Stop.Begin();

            //this.OnClick("Clicked", "Previous");
        }

        public void ResizeWidth(double widthInPixels)
        {
            try
            {
                //canMediaButtons.Width = widthInPixels;
                //bottomFooterBkgStrip.Width = widthInPixels - 12;
                //bottomFooterBkgStrip.SetValue(Canvas.LeftProperty, ((458 * widthInPixels - 12) / 1112.074) + 1);
                //bottomFooterBkgStripGlass.Width = bottomFooterBkgStrip.Width - 4;
                //bottomFooterBkgStripGlass.SetValue(Canvas.LeftProperty, ((458 * widthInPixels - 12) / 1112.074) + 1 + 1);


                //bottomFooterButtonBkg.Width = widthInPixels * 379 / 1116.168;
                //double leftCentering = (canMediaButtons.Width - bottomFooterButtonBkg.Width) / 2;
                //bottomFooterButtonBkg.SetValue(Canvas.LeftProperty, leftCentering);


                //((Rectangle)canMediaProgress.Children[0]).Width = canMediaButtons.Width - 40;
                //((Rectangle)canMediaProgress.Children[1]).Width = canMediaButtons.Width - 40;
                //((Rectangle)canMediaProgress.Children[0]).SetValue(Canvas.LeftProperty, 0);
                //((Rectangle)canMediaProgress.Children[1]).SetValue(Canvas.LeftProperty, 1.125);


                //leftCentering = (canMediaButtons.Width - canButtons.Width) / 2;
                //canButtons.SetValue(Canvas.LeftProperty, leftCentering);

                //leftCentering = (canMediaButtons.Width - bottomFooterButtonBkgCircleBkg.Width) / 2;
                //bottomFooterButtonBkgCircleBkg.SetValue(Canvas.LeftProperty, leftCentering);

                ////leftCentering = (canMediaButtons.Width - canMediaProgress.Width) / 2;
                //canMediaProgress.SetValue(Canvas.LeftProperty, 17);
            }
            catch { }
        }

        private void OnClick(string action, string value)
        {
            if (this.PlayClicked != null)
            {
                this.PlayClicked(new CallbackEventArgs(action, value));
            }
        }

        public void SetPreviousEnable(bool isEnable)
        {
            this.btnPrevious.IsEnabled = isEnable;
        }
        public void SetNextEnable(bool isEnable)
        {
            this.btnNext.IsEnabled = isEnable;
        }

        private void btnNext_Clicked(object sender, System.Windows.RoutedEventArgs e)
        {
            this.OnClick("Clicked", "Next");
        }

        private void btnPrevious_Clicked(object sender, System.Windows.RoutedEventArgs e)
        {
            this.OnClick("Clicked", "Previous");
        }

        private void btnNext_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (this.NextMouseEnter != null)
            {
                this.NextMouseEnter(sender, e);
            }
        }


        private void btnNext_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {

            if (this.NextMouseLeave != null)
            {
                this.NextMouseLeave(sender, e);
            }
        }

        private void btnPrevious_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (this.PreviousMouseEnter != null)
            {
                this.PreviousMouseEnter(sender, e);
            }
        }


        private void btnPrevious_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {

            if (this.PreviousMouseLeave != null)
            {
                this.PreviousMouseLeave(sender, e);
            }
        }
    }

    public delegate void CallbackDelegate(CallbackEventArgs arg);

    public class CallbackEventArgs : EventArgs
    {
        // Fields
        private string _action;
        private string _value;

        // Methods
        public CallbackEventArgs(string Action, string Value)
        {
            this._action = Action;
            this._value = Value;
        }

        // Properties
        public string Action
        {
            get
            {
                return this._action;
            }
        }

        public string Value
        {
            get
            {
                return this._value;
            }
        }
    }





}
